//
//  JKBombView.m
//  JKOCTools
//
//  Created by 朱凯 on 2017/11/25.
//  Copyright © 2017年 JooKey. All rights reserved.
//

#import "JKBombView.h"

@implementation JKBombView
{
}

- (void)dealloc {
    
    NSLog(@"已销毁");
}

#pragma mark - 初始化
- (instancetype)init {
    
    if (self = [super init]) {
        
        // 设置默认值
        _position = JKBombViewOriginPositionTop | JKBombViewTargetPositionTop;
        _shadowUserInteractionEnabled = true;
        _shadowAlpha = 0.2;
        _offset = 0;
        _damping = 1;
        _duration = 0.25;
        
        // 给阴影部分添加点击事件
        [self addTarget:self action:@selector(touchShadow) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}


#pragma mark - 点击事件
- (void)touchShadow {
    
    if (!_shadowUserInteractionEnabled) {
        return;
    }
    [self hide];
}



#pragma mark - 全局事件
- (void)show {

    // 没有内容视图，直接返回
    if (!_contentView) {
        return;
    }
    
    UIWindow *window = ((AppDelegate *)[UIApplication sharedApplication].delegate).window;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    self.frame = window.bounds;
    [window addSubview:self];
    
    [self addSubview:_contentView];
    
    [self makeAnimationForShow];
}
- (void)hide {
    
    [self makeAnimationForHide:^{
        [self removeFromSuperview];
    }];
    
}

#pragma mark - 辅助处理代码块（用于合并大量的代码块）
- (void)makeAnimationForShow {
    
    dispatch_block_t originConfigSetting = NULL;
    dispatch_block_t targetConfigSetting = NULL;
    if (_position & JKBombViewTargetPositionTop) {
        originConfigSetting = ^ {
            _contentView.bottom = 0;
            _contentView.x = self.width / 2;
        };
        targetConfigSetting = ^ {
            _contentView.top = _offset;
        };
    } else if (_position & JKBombViewTargetPositionBottom) {
        originConfigSetting = ^ {
            _contentView.top = self.height;
            _contentView.x = self.width / 2;
        };
        targetConfigSetting = ^ {
            _contentView.bottom = self.height - _offset;
        };
    } else {
        if (_position & JKBombViewOriginPositionTop) {
            originConfigSetting = ^ {
                _contentView.bottom = 0;
                _contentView.x = self.width / 2;
            };
            targetConfigSetting = ^ {
                _contentView.y = self.height / 2 + _offset;
            };
        } else if (_position & JKBombViewOriginPositionBottom) {
            originConfigSetting = ^ {
                _contentView.top = self.height;
                _contentView.x = self.width / 2;
            };
            targetConfigSetting = ^ {
                _contentView.y = self.height / 2 + _offset;
            };
        } else {
            originConfigSetting = ^ {
                _contentView.x = self.width / 2;
                _contentView.y = self.height / 2;
                _contentView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
            };
            targetConfigSetting = ^ {
                _contentView.transform = CGAffineTransformIdentity;
            };
        }
    }
    
    originConfigSetting();
    [UIView animateWithDuration:_duration delay:0 usingSpringWithDamping:_damping initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:_shadowAlpha];
        targetConfigSetting();
    } completion:^(BOOL finished) {
        // nothing
    }];
}
- (void)makeAnimationForHide:(dispatch_block_t)completionBlock {
    
    dispatch_block_t targetConfigSetting = NULL;
    if (_position & JKBombViewTargetPositionTop) {
        targetConfigSetting = ^ {
            _contentView.bottom = 0;
        };
    } else if (_position & JKBombViewTargetPositionBottom) {
        targetConfigSetting = ^ {
            _contentView.top = self.height;
        };
    } else {
        if (_position & JKBombViewOriginPositionTop) {
            targetConfigSetting = ^ {
                _contentView.bottom = 0;
            };
        } else if (_position & JKBombViewOriginPositionBottom) {
            targetConfigSetting = ^ {
                _contentView.top = self.height;
            };
        } else {
            targetConfigSetting = ^ {
                _contentView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.01, 0.01);
            };
        }
    }
    
    [UIView animateWithDuration:_duration delay:0 usingSpringWithDamping:_damping initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        targetConfigSetting();
    } completion:^(BOOL finished) {
        if (finished && completionBlock) {
            completionBlock();
        }
        
    }];
}

#pragma mark - setter
// 检测阴影的透明度值的合法性
- (void)setShadowAlpha:(CGFloat)shadowAlpha {
    
    if (shadowAlpha > 1) {
        _shadowAlpha = 1;
    } else if (shadowAlpha < 0) {
        _shadowAlpha = 0;
    } else {
        _shadowAlpha = shadowAlpha;
    }
}
// 检测劲度系数的合法性
- (void)setDamping:(CGFloat)damping {
    
    if (damping > 1) {
        _damping = 1;
    } else if (damping < 0) {
        _damping = 0;
    } else {
        _damping = damping;
    }
}
// 检测position的完整度
- (void)setPosition:(JKBombViewPosition)position {
    
    // 取出position中的targetPosition
    JKBombViewPosition targetPosition = position & ~(JKBombViewOriginPositionTop | JKBombViewOriginPositionMid | JKBombViewOriginPositionBottom);
    // targetPosition为0，说明没有填写，抛出异常
    if (!targetPosition) {
        NSAssert(nil, @"JKBombViewPosition枚举必须包含targetPosition，或使用便利枚举值");
        return;
    }
    // 先判断目标位置，如果是贴上，或者贴下，则originPosition也与之对应
    if (targetPosition & JKBombViewTargetPositionTop) {
        _position = JKBombViewTargetPositionTop | JKBombViewOriginPositionTop;
    } else if (targetPosition & JKBombViewTargetPositionBottom) {
        _position = JKBombViewTargetPositionBottom | JKBombViewOriginPositionBottom;
    } else {
        // 如果是居中，则判断originPosition的值
        unsigned long originPosition = position & ~(JKBombViewTargetPositionTop | JKBombViewTargetPositionMid | JKBombViewTargetPositionBottom);
        // 如果没有填写originPosition相关的枚举值，则默认是mid
        if (originPosition == 0) {
            originPosition = JKBombViewOriginPositionMid;
        } else if (originPosition & JKBombViewOriginPositionTop) {
            originPosition = JKBombViewOriginPositionTop;
        } else if (originPosition & JKBombViewOriginPositionMid) {
            originPosition = JKBombViewOriginPositionMid;
        } else {
            originPosition = JKBombViewOriginPositionBottom;
        }
        _position = JKBombViewTargetPositionMid | originPosition;
    }
    
    NSLog(@"%lu", (unsigned long)_position);
}

@end
